package com.ruoyi.web.controller.wbgame;

import java.util.List;

import com.ruoyi.wbgame.domain.WbItemType;
import com.ruoyi.wbgame.domain.WbQuestionType;
import com.ruoyi.wbgame.service.IWbItemTypeService;
import com.ruoyi.wbgame.service.IWbQuestionTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wbgame.domain.WbQuestion;
import com.ruoyi.wbgame.service.IWbQuestionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问题Controller
 * 
 * @author guguofu
 * @date 2021-04-16
 */
@Controller
@RequestMapping("/wbgame/question")
public class WbQuestionController extends BaseController
{
    private String prefix = "wbgame/question";

    @Autowired
    private IWbQuestionService wbQuestionService;
    @Autowired
    private IWbQuestionTypeService wbQuestionTypeService;
    @Autowired
    IWbItemTypeService wbItemTypeService;

    @RequiresPermissions("wbgame:question:view")
    @GetMapping()
    public String question()
    {
        return prefix + "/question";
    }

    /**
     * 查询问题列表
     */
    @RequiresPermissions("wbgame:question:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WbQuestion wbQuestion)
    {
        startPage();
        List<WbQuestion> list = wbQuestionService.selectWbQuestionList(wbQuestion);
        return getDataTable(list);
    }

    /**
     * 导出问题列表
     */
    @RequiresPermissions("wbgame:question:export")
    @Log(title = "问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WbQuestion wbQuestion)
    {
        List<WbQuestion> list = wbQuestionService.selectWbQuestionList(wbQuestion);
        ExcelUtil<WbQuestion> util = new ExcelUtil<WbQuestion>(WbQuestion.class);
        return util.exportExcel(list, "question");
    }

    /**
     * 新增问题
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<WbQuestionType> questionTypeList = wbQuestionTypeService.selectWbQuestionTypeList(null); // 知识点类型
        List<WbItemType> wbItemTypeList = wbItemTypeService.selectWbItemTypeList(null);                  // 题型
        model.addAttribute("wbQuestionTypeList",questionTypeList);
        model.addAttribute("wbItemTypeList",wbItemTypeList);
        return prefix + "/add";
    }

    /**
     * 新增保存问题
     */
    @RequiresPermissions("wbgame:question:add")
    @Log(title = "问题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WbQuestion wbQuestion)
    {
          // 测试提交数据的情况
//        System.out.println("------------------");
//        System.out.println(wbQuestion.toString());
        return toAjax(wbQuestionService.insertWbQuestion(wbQuestion));
    }

    /**
     * 修改问题
     */
    @GetMapping("/edit/{questionId}")
    public String edit(@PathVariable("questionId") Long questionId, ModelMap mmap)
    {
        WbQuestion wbQuestion = wbQuestionService.selectWbQuestionById(questionId);  // 问题内容
        List<WbQuestionType> wbQuestionTypeList = wbQuestionTypeService.selectWbQuestionTypeList(null);// 知识点类型
        List<WbItemType> wbItemTypeList = wbItemTypeService.selectWbItemTypeList(null); // 题型id
        mmap.put("wbQuestion", wbQuestion);
        mmap.put("wbQuestionTypeList",wbQuestionTypeList);
        mmap.put("wbItemTypeList",wbItemTypeList);
//        System.out.println(wbQuestion);
        return prefix + "/edit";
    }

    /**
     * 修改保存问题
     */
    @RequiresPermissions("wbgame:question:edit")
    @Log(title = "问题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WbQuestion wbQuestion)
    {
        return toAjax(wbQuestionService.updateWbQuestion(wbQuestion));
    }

    /**
     * 删除问题
     */
    @RequiresPermissions("wbgame:question:remove")
    @Log(title = "问题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wbQuestionService.deleteWbQuestionByIds(ids));
    }
}
